package com.example.service.implService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.ResponseVO;
import com.example.mapper.ClassesMapper;
import com.example.mapper.HomeworkDraftMapper;
import com.example.mapper.HomeworkMapper;
import com.example.pojo.Classes;
import com.example.pojo.Homework;
import com.example.pojo.HomeworkDraft;
import com.example.service.HomeworkDraftService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author GovQ
 * @description 针对表【homework_draft】的数据库操作Service实现
 * @createDate 2024-06-18 16:07:08
 */
@Service
public class HomeworkDraftServiceImpl implements HomeworkDraftService {
    @Resource
    private HomeworkMapper homeworkMapper;

    @Resource
    private HomeworkDraftMapper homeworkDraftMapper;

    @Resource
    private ClassesMapper classesMapper;

    @Override
    public ResponseEntity<ResponseVO<String>> addHomeworkDraft(Homework homework) {
        // 插入作业信息
        homeworkMapper.insert(homework);

        // 获取刚刚插入的homework的ID
        Long homeworkId = homework.getHomeworkId(); // MyBatis Plus会自动填充主键值
        // 创建并插入草稿信息
        HomeworkDraft draft = new HomeworkDraft();
        draft.setHomeworkId(homeworkId);
        draft.setTeacherId(homework.getReleaseId());
        int insert = homeworkDraftMapper.insert(draft);
        if(insert>0){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "添加草稿成功"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "添加草稿失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<HomeworkDraft>> getHomeworkDraftByTeacherId(Long teacherId) {
        QueryWrapper<HomeworkDraft> homeworkDraftQueryWrapper = new QueryWrapper<>();
        HomeworkDraft homeworkDraft = homeworkDraftMapper.selectOne(homeworkDraftQueryWrapper.eq("teacher_id", teacherId));
        if(!Objects.isNull(homeworkDraft)){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "您有草稿尚未发布，是否进入草稿并继续编辑未完成的内容",homeworkDraft));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "没有草稿"));
    }

    @Override
    public ResponseEntity<ResponseVO<String>> deleteHomeworkDraft(HomeworkDraft homeworkDraft) {
        int i = homeworkDraftMapper.deleteById(homeworkDraft);
        if(i>0){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "删除成功"));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "删除失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<Homework>> getHomeworkByDraftId(HomeworkDraft homeworkDraft) {
        Homework homework = homeworkMapper.selectById(homeworkDraft.getHomeworkId());
        if(!Objects.isNull(homework)){
            // 查询出来直接删除掉草稿
            deleteHomeworkDraft(homeworkDraft);
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "操作草稿成功",homework));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "操作失败"));
    }

    @Override
    public ResponseEntity<ResponseVO<List<Classes>>> getPushClassByTeacherId(Long teacherId) {
        QueryWrapper<Classes> classesQueryWrapper = new QueryWrapper<>();
        classesQueryWrapper.eq("head_teacher_id",teacherId).or().eq("assistant_teacher_id",teacherId);
        List<Classes> classes = classesMapper.selectList(classesQueryWrapper);
        if(!classes.isEmpty()){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "查询成功",classes));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "您还没有可推送的班级"));
    }
}




